using UnityEngine;
using Unity.VisualScripting;

namespace LS.VisualScripting
{
    public class PlaySound : SoundUnit
    {
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput SoundId { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Position { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput UserData { get; set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput SerialId { get; set; }
        
        protected override void Definition()
        {
            base.Definition();
            SoundId = ValueInput<int>(nameof(SoundId), 0);
            Position =  ValueInput<Vector3>(nameof(Position), Vector3.zero);
            UserData = ValueInput<object>(nameof(UserData), null);
            SerialId = ValueOutput<int>(nameof(SerialId));
            Requirement(SoundId, Input);
            Requirement(Position, Input);
            Requirement(UserData, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            int soundId = flow.GetValue<int>(SoundId);
            Vector3 pos = flow.GetValue<Vector3>(Position);
            object userData = flow.GetValue<object>(UserData);
            int serialId = Target?.PlaySound(soundId, pos, userData) ?? 0;
            flow.SetValue(SerialId, serialId);
            return base.In(flow);
        }
    }
}